package org.dreamwork.jasmine2.web.controls;

import org.dreamwork.jasmine2.engine.IPostbackHandler;
import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.util.NameValueCollection;
import org.dreamwork.peony.DataBinder;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-28
 * Time: 20:12:19
 */
public class DatePicker extends WebControl implements IPostbackHandler {
    private Date date;
    private String text;
    private String pattern;
    private SimpleDateFormat format;
    private boolean showTime = false;

    private final Pattern datePattern = Pattern.compile ("[\\d]{4}-[\\d]{2}-[\\d]{2}");
    private final Pattern timePattern = Pattern.compile ("[\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}");

    public DatePicker () {
        htmlTagName = "input";
        attrs.setProperty ("readonly", "readonly");
    }

    public Date getDate () {
        return date;
    }

    public void setDate (Date date) {
        if (date == null) {
            this.date = null;
            this.text = "";
        } else {
            this.date = date;
            if (pattern == null || StringConverter.isEmpty (pattern)) {
                pattern = showTime ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd";
            }
            if (pattern != null) {
                format = new SimpleDateFormat (pattern);
                this.text = format.format (date);
            }
        }
    }

    public boolean isShowTime () {
        return showTime;
    }

    public void setShowTime (boolean showTime) {
        this.showTime = showTime;
    }

    public String getText () {
        return text;
    }

    public void setText (String text) {
        if (text == null) return;
        this.text = text;
        if (pattern == null || StringConverter.isEmpty (text)) {
            Matcher m = datePattern.matcher (text);
            if (m.matches ()) pattern = "yyyy-MM-dd";
            else {
                m = timePattern.matcher (text);
                if (m.matches ()) pattern = "yyyy-MM-dd HH:mm:ss";
            }            
        }
        if (pattern != null && !StringConverter.isEmpty (text)) {
            format = new SimpleDateFormat (pattern);
            try {
                date = format.parse (text);
            } catch (ParseException e) {
                e.printStackTrace ();
            }
        }
    }

    public String getPattern () {
        return pattern;
    }

    public void setPattern (String pattern) {
        this.pattern = pattern;
        if (pattern != null) {
            setText (text);
        }
    }

    public void init () {
        super.init ();
        page.registerClientScriptBlock (InternalJS.class, InternalJS.UTIL_JS);
        page.registerClientScriptBlock (InternalJS.class, InternalJS.CALENDAR2_JS);
        page.registerStyleFile (InternalJS.class, InternalJS.CALENDAR_CSS);
    }

    @Override
    public void onDataBind (Object sender) throws EventException {
        if (dataSource != null && valueField != null) {
            Object value = DataBinder.eval (dataSource, valueField);
            if (value != null) {
                this.setDate ((Date) value);
            } else {
                setDate (null);
            }
        }
    }

    public void postbackData (NameValueCollection props) {
        String text = props.getValue (getName ());
        setText (text);
    }

    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        if (!StringConverter.isEmpty (text))
            attrs.setProperty ("value", text);
        page.addStartupStatement ("var __calendar = new org.dreamwork.js.util.Calendar2 ();\r\n");
        page.addStartupStatement (String.format ("__calendar.install ('%s'%s);%n", id, showTime ? ", true" : ""));        
    }
}